sort_list = "This is a waiting sorted List".split()
print(sort_list)
# key传递一个比较规则的函数
print(sorted(sort_list, key=str.lower))  # 排序列表
print('-' * 50)
student_tuples = [
    ('jane', 'B', 12),
    ('john', 'A', 15),
    ('dave', 'B', 10),
]
# lambda表达式：匿名函数。提高编写效率，提高阅读速度
print(sorted(student_tuples, key=lambda x: x[1]))  # 排序列表中元组
print('-' * 50)


class Student:
    def __init__(self, name, address, age):
        self.name = name
        self.address = address
        self.age = age

    def __repr__(self):
        """
        可以返回非字符串类型
        """
        return repr((self.name, self.address, self.age))


student = Student('小明', '北京', 21)
student_object = [
    Student('小明', '北京', 21),
    Student('白叶', '天津', 17),
    Student('孙楠', '上海', 24)
]
print(sorted(student_object, key=lambda student: student.address))  # 对类对象进行排序
print('-' * 50)

from operator import itemgetter, attrgetter

print('使用operator:')
print(sorted(student_tuples, key=itemgetter(2)))
print(sorted(student_object, key=attrgetter('age')))
print('-' * 50)
print('使用operator，多列排序')
print(sorted(student_tuples, key=lambda x: (x[1], -x[2])))  # 第一列升序，第二列降序
print(sorted(student_object, key=attrgetter('age', 'name'), reverse=True))
print(sorted(student_tuples, key=itemgetter(2, 1)))
